{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Capitalize the Title"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: capitalizeTitle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将标题首字母大写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串&nbsp;<code>title</code>&nbsp;，它由单个空格连接一个或多个单词组成，每个单词都只包含英文字母。请你按以下规则将每个单词的首字母 <strong>大写</strong>&nbsp;：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果单词的长度为&nbsp;<code>1</code>&nbsp;或者&nbsp;<code>2</code>&nbsp;，所有字母变成小写。</li>\n",
    "\t<li>否则，将单词首字母大写，剩余字母变成小写。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回 <strong>大写后</strong>&nbsp;的<em>&nbsp;</em><code>title</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><b>示例 1：</b></p>\n",
    "\n",
    "<pre><b>输入：</b>title = \"capiTalIze tHe titLe\"\n",
    "<b>输出：</b>\"Capitalize The Title\"\n",
    "<strong>解释：</strong>\n",
    "由于所有单词的长度都至少为 3 ，将每个单词首字母大写，剩余字母变为小写。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>title = \"First leTTeR of EACH Word\"\n",
    "<b>输出：</b>\"First Letter of Each Word\"\n",
    "<strong>解释：</strong>\n",
    "单词 \"of\" 长度为 2 ，所以它保持完全小写。\n",
    "其他单词长度都至少为 3 ，所以其他单词首字母大写，剩余字母小写。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>title = \"i lOve leetcode\"\n",
    "<b>输出：</b>\"i Love Leetcode\"\n",
    "<strong>解释：</strong>\n",
    "单词 \"i\" 长度为 1 ，所以它保留小写。\n",
    "其他单词长度都至少为 3 ，所以其他单词首字母大写，剩余字母小写。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= title.length &lt;= 100</code></li>\n",
    "\t<li><code>title</code>&nbsp;由单个空格隔开的单词组成，且不含有任何前导或后缀空格。</li>\n",
    "\t<li>每个单词由大写和小写英文字母组成，且都是 <strong>非空</strong>&nbsp;的。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [capitalize-the-title](https://leetcode.cn/problems/capitalize-the-title/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [capitalize-the-title](https://leetcode.cn/problems/capitalize-the-title/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"capiTalIze tHe titLe\"', '\"First leTTeR of EACH Word\"', '\"i lOve leetcode\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words = title.split(\" \")\n",
    "        for i, w in enumerate(words):\n",
    "            length = len(w)\n",
    "            if length == 1 or length == 2:\n",
    "                words[i] = w.lower()\n",
    "            else:\n",
    "                words[i] = w[0:1].upper() + w[1:].lower()\n",
    "        return \" \".join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        title = title.title()\n",
    "        result = []\n",
    "        words = title.split()\n",
    "        for i in words:\n",
    "            if len(i) <= 2:\n",
    "                result.append(i.lower())\n",
    "            else:\n",
    "                result.append(i)\n",
    "        return ' '.join(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        t_l = title.split()\n",
    "        new_title = []\n",
    "        for i in t_l:\n",
    "            if len(i) > 2:\n",
    "                new_title.append(i.capitalize())\n",
    "            else:\n",
    "                new_title.append(i.lower())\n",
    "        return \" \".join(new_title)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        result = []\n",
    "        for word in title.split(\" \"):\n",
    "            if len(word) <= 2:\n",
    "                text = word.lower()\n",
    "            else:\n",
    "                text = word[:1].upper() + word[1:].lower()\n",
    "            \n",
    "            result.append(text)\n",
    "        \n",
    "        return \" \".join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        lst = []\n",
    "        for word in title.split(\" \"):\n",
    "            if len(word) <= 2:\n",
    "                lst.append(word.lower())\n",
    "            else:\n",
    "                lst.append(word.lower().capitalize())\n",
    "        return \" \".join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        ans_list = title.split(' ')\n",
    "        for i in range(len(ans_list)):\n",
    "            if len(ans_list[i]) <= 2:\n",
    "                ans_list[i] = ans_list[i].lower()\n",
    "            else:\n",
    "                ans_list[i] = ans_list[i].title()\n",
    "        return ' '.join(ans_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        title = title.split()\n",
    "        for i in range(len(title)):\n",
    "            if len(title[i]) > 2:\n",
    "                title[i] = title[i].title()\n",
    "            else:\n",
    "                title[i] = title[i].lower()\n",
    "        return ' '.join(title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = []\n",
    "        for word in title.split():\n",
    "            if len(word) <= 2:\n",
    "               res.append(word.lower())\n",
    "            else:\n",
    "                res.append(word[0].upper() + word[1:].lower())\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = []   # 辅助数组\n",
    "        for word in title.split():\n",
    "            # 对于分割的每个单词按要求处理\n",
    "            if len(word) <= 2:\n",
    "                res.append(word.lower())\n",
    "            else:\n",
    "                res.append(word[0].upper() + word[1:].lower())\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = [] \n",
    "        for word in title.split() :\n",
    "            if len(word) <= 2 :\n",
    "                res.append(word.lower())\n",
    "            else :\n",
    "                res.append(word[0].upper() + word[1:].lower())\n",
    "        return ' '.join(res) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        word_list = title.split()\n",
    "        capitalized_words = [word.title() if len(word) > 2 else word.lower() for word in word_list]\n",
    "        return ' '.join(capitalized_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        word_list = title.split()\n",
    "        capitalized_words = [word.title() if len(word) > 2 else word.lower() for word in word_list]\n",
    "        return ' '.join(capitalized_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = []\n",
    "        for word in title.split():\n",
    "            if len(word)<=2:\n",
    "                res.append(word.lower())\n",
    "            else:\n",
    "                res.append(word[0].upper()+word[1:].lower())\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words = title.split(\" \")\n",
    "        res = \"\"\n",
    "        for w in words:\n",
    "            if len(w) <= 2:\n",
    "                res += w.lower()\n",
    "            else:\n",
    "                res += w.capitalize()\n",
    "            res += \" \"\n",
    "        return res[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join(i.capitalize() if len(i) > 2 else i.lower() for i in title.split())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        arr = title.split(' ')\n",
    "        ans = []\n",
    "        for i in arr:\n",
    "            if len(i)<3:\n",
    "                ans.append(i.casefold())\n",
    "            else:\n",
    "                ans.append(i.capitalize())\n",
    "        return \" \".join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        def zhuanhuan(s):\n",
    "            if len(s)<=2:\n",
    "                return s.lower()\n",
    "            else:\n",
    "                return s[0].upper()+s[1:].lower()  \n",
    "        s = \"\"        \n",
    "        for t in title.split(\" \"):          \n",
    "            s =s+\" \"+zhuanhuan(t)\n",
    "        return s[1:len(s)]    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        sList = title.split(\" \")\n",
    "        s = \"\"\n",
    "        for item in sList:\n",
    "            if len(item)<3:\n",
    "                s+=item.lower()+' '\n",
    "                continue\n",
    "            tmp = item.lower()\n",
    "            tmp = tmp.replace(tmp[0],tmp[0].upper(),1)\n",
    "            s+=tmp+' '\n",
    "        return s[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join(s.capitalize() if len(s) > 2 else s.lower() for s in title.split())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        l=title.split()\n",
    "        for i in range(len(l)):\n",
    "            if len(l[i])>2:\n",
    "                l[i]=l[i].lower().capitalize()\n",
    "            else:\n",
    "                l[i]=l[i].lower()\n",
    "        return ' '.join(l)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words = title.split() \n",
    "        ans = \"\"\n",
    "        for word in words :\n",
    "            if len(word) <= 2 : \n",
    "                ans += word.lower() + \" \"\n",
    "            else :\n",
    "                tmp = word.lower() \n",
    "                tmp = tmp[0].upper()+tmp[1:]\n",
    "                ans += tmp + \" \"\n",
    "        return ans[:len(ans)-1] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = []   # 辅助数组\n",
    "        for word in title.split():\n",
    "            # 对于分割的每个单词按要求处理\n",
    "            if len(word) <= 2:\n",
    "                res.append(word.lower())\n",
    "            else:\n",
    "                res.append(word[0].upper() + word[1:].lower())\n",
    "        return ' '.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = []   # 辅助数组\n",
    "        for word in title.split():\n",
    "            # 对于分割的每个单词按要求处理\n",
    "            if len(word) <= 2:\n",
    "                res.append(word.lower())\n",
    "            else:\n",
    "                res.append(word[0].upper() + word[1:].lower())\n",
    "        return ' '.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        title_s = title.split()\n",
    "        title_final = \"\"\n",
    "        for t in title_s:\n",
    "            if len(t) > 2:\n",
    "                title_final += t.title() + \" \"\n",
    "            else:\n",
    "                title_final += t.lower() + \" \"\n",
    "        return title_final.rstrip()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        ls=title.split(\" \")\n",
    "        for i,w in enumerate(ls):\n",
    "            w=w.lower()\n",
    "            if len(w)>2:\n",
    "                w=w[0].upper()+w[1:]\n",
    "            ls[i]=w\n",
    "        return \" \".join(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words=title.split(' ')\n",
    "        res=[]\n",
    "        for word in words:          \n",
    "            if len(word)>2:\n",
    "                word=word.lower()\n",
    "                word=word.capitalize()\n",
    "            else:\n",
    "                word=word.lower()\n",
    "            res.append(word)\n",
    "        return  ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        title = title.title()\n",
    "        words = title.split()\n",
    "        for i in range(len(words)):\n",
    "            if len(words[i]) <= 2:\n",
    "                words[i] = words[i].lower()\n",
    "        return ' '.join(words)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        title = title.lower().split()\n",
    "        res = []\n",
    "        for word in title:\n",
    "            if len(word)<=2:res.append(word)\n",
    "            else:res.append(word[0].upper()+word[1:])\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        str_list = title.split(' ')\n",
    "        for i, _str in enumerate(str_list):\n",
    "            if len(_str) >= 3:\n",
    "                str_list[i] = _str[0].upper() + _str[1::].lower()\n",
    "            else:\n",
    "                str_list[i] = _str.lower()\n",
    "        return \" \".join(x for x in str_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        titleList = title.split(' ')\n",
    "        for i in range(len(titleList)):\n",
    "            if len(titleList[i]) <= 2:\n",
    "                titleList[i] = titleList[i].lower()\n",
    "            else:\n",
    "                titleList[i] = titleList[i][0].upper() + titleList[i][1:].lower()\n",
    "        return ' '.join(titleList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words=title.split()\n",
    "        for i,x in enumerate(words):\n",
    "            x=x.lower()\n",
    "            if len(x)>=3:\n",
    "                x=x[0].upper()+x[1:]\n",
    "            words[i]=x\n",
    "        ans=''\n",
    "        for i in words:\n",
    "            ans+=i+' '\n",
    "        return ans.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words = title.split(\" \")\n",
    "        s = \"\"\n",
    "        for word in words:\n",
    "            s += \" \"\n",
    "            if len(word) <= 2:\n",
    "                s += word.lower()\n",
    "            else:\n",
    "                s += (word[0].upper() + word[1:].lower())\n",
    "        return s[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join([i.lower() if len(i)<3 else i.capitalize() for i in title.split()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words = title.split(\" \")\n",
    "        for index, word in enumerate(words):\n",
    "            if len(word) <= 2:\n",
    "                words[index] = word.lower()\n",
    "            else:\n",
    "                words[index] = word.lower().title()\n",
    "        return \" \".join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = \"\"\n",
    "        for i in title.split():\n",
    "            res += i[0].upper()+i[1:].lower() if len(i) > 2 else i.lower()\n",
    "            res += \" \"\n",
    "        return res.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        ans = []\n",
    "        ts = title.split(' ')\n",
    "        for idx in range(len(ts)):\n",
    "            if len(ts[idx]) <= 2:\n",
    "                ans.append(ts[idx].lower())\n",
    "            else:\n",
    "                ans.append(ts[idx].title())\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join([ word.title() if len(word) > 2 else word.lower() for word in title.split() ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        a=title.split(\" \")\n",
    "        b=[]\n",
    "        for i in a:\n",
    "            if len(i)<=2:\n",
    "                s=\"\"\n",
    "                for x in i:\n",
    "                    if x.islower():\n",
    "                        s=s+x\n",
    "                    else:\n",
    "                        s=s+x.lower()\n",
    "                b.append(s)\n",
    "\n",
    "            else:\n",
    "                s=\"\"\n",
    "                if i[0].islower():\n",
    "                    s=s+i[0].upper()\n",
    "                else:\n",
    "                    s=s+i[0]\n",
    "                for x in range(1,len(i)):\n",
    "                    if i[x].isupper:\n",
    "                        s=s+i[x].lower()\n",
    "                    else:\n",
    "                        s=s+i[x]\n",
    "                b.append(s)\n",
    "        s=\"\"\n",
    "        for i in range(0,len(b)):\n",
    "            s=s+b[i]+\" \"\n",
    "        return s[0:len(s)-1]\n",
    "        \n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words=title.split(' ')\n",
    "        for i in range(0,len(words)):\n",
    "            if len(words[i])<=2:\n",
    "                words[i]=words[i].lower()\n",
    "            else:\n",
    "                words[i]=words[i][0].upper()+words[i][1:].lower()\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        ans = []\n",
    "        ts = title.split(' ')\n",
    "        for idx in range(len(ts)):\n",
    "            if len(ts[idx]) <= 2:\n",
    "                ans.append(ts[idx].lower())\n",
    "            else:\n",
    "                ans.append(ts[idx].title())\n",
    "        return ' '.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words=title.split()\n",
    "        #print(words)\n",
    "        new=[]\n",
    "        for word in words:\n",
    "            if len(word)<=2:\n",
    "                new.append(word.lower())\n",
    "            else:\n",
    "                new.append(word[0].upper()+word[1:].lower())\n",
    "        return (' '.join(new))  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        final_title = ''\n",
    "        tmp = ''\n",
    "        for c in title:\n",
    "            if c == ' ':\n",
    "                if len(tmp) <= 2:\n",
    "                    final_title += tmp.lower()\n",
    "                else:\n",
    "                    final_title += tmp[0].upper()\n",
    "                    final_title += tmp[1:].lower()\n",
    "                tmp = ''\n",
    "                final_title += c\n",
    "            else:\n",
    "                tmp += c\n",
    "        \n",
    "        if len(tmp) <= 2:\n",
    "            final_title += tmp.lower()\n",
    "        else:\n",
    "            final_title += tmp[0].upper()\n",
    "            final_title += tmp[1:].lower()\n",
    "\n",
    "        return final_title\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words = title.split(' ')\n",
    "        for i in range(len(words)):\n",
    "            if len(words[i]) <= 2:\n",
    "                words[i] = words[i].lower()\n",
    "            else:\n",
    "                words[i] = words[i][0].upper() + words[i][1:].lower()\n",
    "        return ' '.join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join(s.capitalize() if len(s) > 2 else s.lower() for s in title.split())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join(c.capitalize() if len(c) > 2 else c.lower() for c in title.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words=title.split()\n",
    "        #print(words)\n",
    "        new=[]\n",
    "        for word in words:\n",
    "            if len(word)<=2:\n",
    "                new.append(word[:].lower())\n",
    "            else:\n",
    "                new.append(word[0].upper()+word[1:].lower())\n",
    "        return (' '.join(new))  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join(s.capitalize() if len(s)>2 else s.lower() for s in title.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res=[]\n",
    "        for tt in title.split():\n",
    "            if len(tt)<=2:\n",
    "                res.append(tt.lower())\n",
    "            else:\n",
    "                res.append(tt[0].upper()+tt[1:].lower())\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        sList = title.split(\" \")\n",
    "        s = \"\"\n",
    "        for item in sList:\n",
    "            if len(item)<3:\n",
    "                s+=item.lower()+' '\n",
    "                continue\n",
    "            tmp = item.lower()\n",
    "            pre = tmp[0]\n",
    "            aft = tmp[0].upper()\n",
    "            tmp = tmp.replace(pre,aft,1)\n",
    "            s+=tmp+' '\n",
    "        return s[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        return ' '.join(char.capitalize() if len(char)>2 else char.lower() for char in title.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        title = title.split()\n",
    "        for i in range(len(title)):\n",
    "            if len(title[i]) > 2:\n",
    "                title[i] = title[i].title()\n",
    "            else:\n",
    "                print(title[i])\n",
    "                title[i] = title[i].lower()\n",
    "        return ' '.join(title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        titleList=title.split(' ')\n",
    "        # print(titleList)\n",
    "        for i in range(len(titleList)):\n",
    "            if len(titleList[i])<=2:\n",
    "                titleList[i]=titleList[i].lower()\n",
    "            else:\n",
    "                titleList[i]=titleList[i].lower()\n",
    "                titleList[i]=titleList[i][0].upper()+titleList[i][1:]\n",
    "        return ' '.join(titleList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        a1 = title.split(\" \")\n",
    "        answer = []\n",
    "        for value in a1:\n",
    "            if len(value) == 1 or len(value) == 2:\n",
    "                a = value.lower()\n",
    "            else:\n",
    "                a = value.title()\n",
    "            answer.append(a)\n",
    "        b = \" \".join(answer)\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        lst = title.split(' ')\n",
    "        for i in range(len(lst)):\n",
    "            if len(lst[i]) < 3:\n",
    "                lst[i] = lst[i].lower()\n",
    "            else:\n",
    "                lst[i] = lst[i].capitalize()\n",
    "        return ' '.join(lst)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        res = []\n",
    "        for word in title.split():\n",
    "            if len(word) <= 2:\n",
    "                res.append(word.lower())\n",
    "            else:\n",
    "                res.append(word[0].upper() + word[1:].lower())\n",
    "        return ' '.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        titlewords = title.split(' ')\n",
    "        result = ''\n",
    "        for titleword in titlewords:\n",
    "            if len(titleword) < 3:\n",
    "                result += titleword.lower()\n",
    "            else:\n",
    "                result += titleword.capitalize()\n",
    "            result += ' '\n",
    "        return result[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        words = title.split(\" \")  # 将字符串按照空格分割成单词列表\n",
    "        for i in range(len(words)):\n",
    "            word = words[i]\n",
    "            if len(word) == 1 or len(word) == 2:\n",
    "                words[i] = word.lower()  # 如果单词长度为1或2，转换为小写\n",
    "            else:\n",
    "                words[i] = word.capitalize()  # 否则，首字母大写，其余小写\n",
    "                \n",
    "        return \" \".join(words)  # 用空格将处理后的单词连接起来\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def capitalizeTitle(self, title: str) -> str:\n",
    "        title=title.split(\" \")\n",
    "        n=len(title)\n",
    "        for i in range(n):\n",
    "            title[i]=title[i].lower()\n",
    "            if len(title[i])>2:\n",
    "                title[i]=title[i][0].upper()+title[i][1:]\n",
    "        return \" \".join(title)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
